package myTest.sort;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class GetAllFile {
	public static void main(String[] args) throws IOException {
		String key = "jar";
		String path = "/home/chenwei/Documents/";
		String file = "/home/chenwei/Document/hahaha";

		File dir = new File(path);
		getAll(dir, key, 0);

		System.out.println(sb);

		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

		bw.write(sb.toString());
		bw.close();
	}

	private static StringBuilder sb = new StringBuilder();

	public static void getAll(File dir, String keyStr, int level) {
		sb.append(getLevel(level) + dir.getName()).append("\n");
		level++;
		File[] files = dir.listFiles();
		for (int x = 0; x < files.length; x++) {
			if (files[x].isDirectory())
				getAll(files[x], keyStr, level);
			else if (files[x].getName().toLowerCase().contains(keyStr.toLowerCase()))
				sb.append(getLevel(level) + files[x].getName()).append("\n");
		}
	}

	public static String getLevel(int level) {
		StringBuilder sb = new StringBuilder();
		sb.append("|--");

		for (int x = 0; x < level; x++) {
			sb.insert(0, "|\t\t");
		}
		return sb.toString();
	}

}